package cn.pkubangbang.demo.democontact.controller;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import cn.pkubangbang.demo.democontact.entity.Person;
import cn.pkubangbang.demo.democontact.interceptor.AuthenticationInterceptor;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * todo 有一个规律要注意：运行集成测试时，应该是不需要Mock的。类似拦截器这种涉及安全问题的阻碍
 * 应该以更巧妙的方式去解决，而不是简单地通过mock来绕过去。
 *
 * @Author: zhangzhenbang@k2data.com.cn
 * @Date: 2018-09-11 13:27
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
// note no need to have mockMvc here, because this is a rest api
//@AutoConfigureMockMvc
public class PersonControllerIT {

    //@Autowired
    //private MockMvc mockMvc;

    // todo how to pass authService when in integration test?
    @MockBean
    //@SpyBean
    private AuthenticationInterceptor interceptor;

    @Autowired
    private TestRestTemplate template;

    @Before
    public void setUp() throws Exception {
        when(interceptor.preHandle(any(), any(), any())).thenReturn(true);
    }

    @Test
    public void testGetPersonById_whenGoodId_willReturnPerson() throws Exception {

        Person xiongDa = new Person(1, "熊大", null, null);

        Person who = template.getForObject("/person/1", Person.class);
        assertEquals(xiongDa, who);
    }
}